﻿using UnityEngine;



namespace UIFramwork.Utils
{
    /// <summary>
    /// 布局助理
    /// </summary>
    public class LayoutUtil
    {
        /// <summary>
        /// 
        /// </summary>
        public static void Reset(Transform transform)
        {
            LayoutUtil.SetTransform(transform);
            LayoutUtil.SetOffset(transform, Vector2.zero, Vector2.zero);
            LayoutUtil.SetAnchor(transform, Vector2.zero, Vector2.one);
        }


        /// <summary>
        /// 设置|遮罩
        /// </summary>
        /// <param name="uiBase"></param>
        public static void SetMask(UIBase uiBase)
        {
            if (!uiBase.UseMask) return;

            //UIUnitPool unitPool = UIAssetsPool.GetUnitPool<>();
        }

        /// <summary>
        /// 设置|布局
        /// </summary>
        public static void SetQueue(UIBase uiBase, Transform root)
        {
            uiBase.gameObject.transform.SetParent(root);
            uiBase.transform.SetAsLastSibling();

            RectTransform @rectTransform = uiBase.GetComponent<RectTransform>();
            @rectTransform.localPosition = Vector3.zero;
            @rectTransform.localRotation = Quaternion.identity;
            @rectTransform.localScale = Vector3.one;
        }

        /// <summary>
        /// 设置|四周型
        /// </summary>
        public static void SetQuard(UIBase uiBase)
        {
            if (!uiBase.UseQuardRato) return;

            LayoutUtil.SetOffset(uiBase.transform, Vector2.zero, Vector2.zero);
            LayoutUtil.SetAnchor(uiBase.transform, Vector2.zero, Vector2.one);
        }




        public static void SetSibling(Transform transform, int index)
        {
            if (index == -1)
            {
                transform.SetAsLastSibling();
                return;
            }

            if (index == 0)
            {
                transform.SetAsFirstSibling();
                return;
            }


            if (index >= transform.parent.childCount)
            {
                Debug.LogWarning($"{index} 大于实际队列数：{transform.parent.childCount}, 默认设置为最后队列");
                transform.SetAsLastSibling();
                return;
            }

            transform.SetSiblingIndex(index);
        }
        public static void SetTransform(Transform transform)
        {
            bool isGet = transform.TryGetComponent(out RectTransform rectTF);

            if (!isGet)
            {
                Debug.LogWarning($"{transform.gameObject.name} 不具备 RectTransform 组件，请检查");
                return;
            }

            rectTF.localPosition = Vector3.zero;
            rectTF.localRotation = Quaternion.identity;
            rectTF.localScale = Vector3.one;
        }
        public static void SetAnchor(Transform transform, Vector2 anchorMax, Vector2 anchorMin)
        {
            bool isGet = transform.TryGetComponent(out RectTransform rectTF);

            if (!isGet)
            {
                Debug.LogWarning($"{transform.gameObject.name} 不具备 RectTransform 组件，请检查");
                return;
            }

            rectTF.anchorMax = anchorMax;
            rectTF.anchorMin = anchorMin;
        }
        public static void SetOffset(Transform transform, Vector2 offsetMax, Vector2 offsetMin)
        {
            bool isGet = transform.TryGetComponent(out RectTransform rectTF);

            if (!isGet)
            {
                Debug.LogWarning($"{transform.gameObject.name} 不具备 RectTransform 组件，请检查");
                return;
            }

            rectTF.offsetMax = offsetMax;
            rectTF.offsetMin = offsetMin;
        }
    }
}
